
const SHOPPING_CART_URL = "http://localhost:8080/gitbug_shop/ShoppingCartController.do"
$(function () {
    var uid = localStorage.getItem("userId");
    var name = localStorage.getItem("name");
    if(uid!=undefined && uid!=null && uid!="" && uid.length>=1){
        if(name!=undefined && name!= null && name!="" && name.length>=1&&name!="undefined"){
            $('#username').append(
                "欢迎"+name
            )
        }else {
            $('#username').append(
                "欢迎游客"
            )
        }
    }else {
        $('#username').append(
            $('<a href="login.html" target="_top" class="h">亲，请登录</a>')
        ).append(
            $('<a href="register.html" target="_top">免费注册</a>')
        )
    }
    var str = localStorage.getItem("userId");
    var id = JSON.parse(str);
    /* if(id==""||id==undefined){
      //   location.href="login.html";
         return false;
     }*/
    function loadData() {
        var params = {
            userId:id,
            anction:"getData"
        }
    }
    loadData();
    function showShoppingCart(data) {
        for (const shoppingCart of data.shoppingCarts) {
            for (const information of data.informations) {
                if(shoppingCart.pId == information.id){
                    $("#cart").append(
                        $('<ul class="item-content clearfix">').append(
                            $('<li class="td td-chk">').append(
                                $('<div class="cart-checkbox">').append(
                                    $('<input class="checked"  name="'+shoppingCart.id+'" value="'+information.id+'" type="checkbox">')
                                ).append(
                                    $('<label for="J_CheckBox_170037950254"></label>')
                                )
                            )
                        ).append(
                            $('<li class="td td-item">').append(
                                $('<div class="item-pic">').append(
                                    $('<a href="../home/introduction.html?commodity_id='+information.id+'" target="_blank" data-title="'+information.shopDiscount+'" class="J_MakePoint" data-point="tbcart.8.12">').append(
                                        $('<img class="itempic J_ItemImg">').attr("src","../images/"+information.image).css("width","80px")
                                    )
                                )
                            ).append(
                                $('<div class="item-info">').append(
                                    $('<div class="item-basic-info">').append(
                                        $('<a href="#" target="_blank" title="" class="item-title J_MakePoint" data-point="tbcart.8.11"></a>')
                                    ).text(information.shopDiscount)
                                )
                            )
                        ).append(
                            $('<li class="td td-info">').append(
                                $('<div class="item-props item-props-can">').append(
                                    $('<span class="sku-line"></span>')
                                ).append(
                                    $('<span class="sku-line"></span>')
                                ).append(
                                    $('<span tabindex="0" class="btn-edit-sku theme-login"></span>')
                                ).append(
                                    $('<i class="theme-login am-icon-sort-desc"></i>')
                                )
                            )
                        ).append(
                            $('<li class="td td-price">').append(
                                $('<div class="item-price price-promo-promo">').append(
                                    $('<div class="price-content">').append(
                                        $('<div class="price-line">').append(
                                            $('<em class="price-original">'+information.originalPrice+'</em>')
                                        )
                                    ).append(
                                        $('<div class="price-line">').append(
                                            $('<em class="J_Price price-now" tabindex="0">'+information.promotiomPrice+'</em>')
                                        )
                                    )
                                )
                            )
                        ).append(
                            $('<li class="td td-amount">').append(
                                $('<div class="amount-wrapper ">').append(
                                    $('<div class="item-amount" value="'+information.id+'">').append(
                                        $('<input  type="button" hidden="hidden" value="'+information.id+'"/>')
                                    ).append(
                                        $('<input class="mins am-btn" name="'+information.id+'" type="button" value="-" />')
                                    ).append(
                                        $('<input class="text_boxs" name="" type="text" value="'+shoppingCart.number+'" style="width:30px;" />')
                                    ).append(
                                        $('<input class="adds am-btn" name="'+information.id+'" type="button" value="+" />')
                                    )
                                )
                            )
                        ).append(
                            $('<li class="td td-sum">').append(
                                $('<div class="td-inner">').append(
                                    $('<em tabindex="0" value="'+(information.promotiomPrice*shoppingCart.number)+'" class="J_ItemSum number money">'+(information.promotiomPrice*shoppingCart.number)+'</em>')
                                )
                            )
                        ).append(
                            $('<li class="td td-op">').append(
                                $('<div class="td-inner">').append(
                                    $('<a title="移入收藏夹" class="btn-fav" href="#">&nbsp;</a>')
                                ).append('<a href="javascript:;" data-point-url="#" name="'+information.id+'" class="delete">删除</a>')
                            )
                        )
                    )
                }
            }

        }
    };
    /**
     * 全选按钮
     */
    $("#all").click(checkall);
    function checkall(){
        all = document.getElementById("all");
        var check = document.getElementsByClassName("checked");
        for (var i = 0; i < check.length; i++) {
            if(all.checked){
                check[i].checked=true;
            }else {
                check[i].checked=false;
            }
        }
        getMoney();
    }

    /**
     * 选中单个
     */
    $(document).on('click','.checked',function(){
        getMoney();
        var check = document.getElementsByClassName("checked");
        for (var i = 0; i < check.length; i++) {
            if(!check[i].checked){
                document.getElementById("all").checked=false;
                return;
            }
        }
        document.getElementById("all").checked=true;
    });
    /**
     * 点击减数量
     */
    $(document).on('click','.mins',function(){
        var commodityId = $(this).attr("name");
        var params = {
            userId:id,
            anction:"updateData",
            commodityId:commodityId,
            number:-1
        }
        $.post(SHOPPING_CART_URL,params,function (result) {
            if(result.status === 200){
                removeDate();
                showShoppingCart(result.data);
            }else {
                showErrorBox("网路异常")
            }
        });
    });
    function removeDate() {
        $("#cart > ul").remove();
    }

    /**
     * 点击添加数量
     */
    $(document).on('click','.adds',function(){
        var commodityId = $(this).attr("name");
        var params = {
            userId:id,
            anction:"updateData",
            commodityId:commodityId,
            number:1
        }
        $.post(SHOPPING_CART_URL,params,function (result) {
            if(result.status === 200){
                removeDate();
                showShoppingCart(result.data);
            }else {
                showErrorBox("网路异常")
            }
        });
    });
    /**
     * 删除
     */
    $(document).on('click','.delete',function(){
        var commodityId = $(this).attr("name");
        var params = {
            userId:id,
            anction:"deleteData",
            commodityIdList:commodityId,
            number:1
        }
        $.post(SHOPPING_CART_URL,params,function (result) {
            if(result.status === 200){
                removeDate();
                showShoppingCart(result.data);
            }else {
                showErrorBox("网路异常")
            }
        });
    });

    /**
     * 删除多个
     *
     */
    $("#deleteAll").click(function () {
        var commodityIdList = [];
        var count = 0;
        var check = document.getElementsByClassName("checked");
        for (let i = 0; i < check.length; i++) {
            if(check[i].checked){
                commodityIdList[count++] = check[i].value;
            }
        }
        if(commodityIdList.length>0){
            var str = "?userId="+id+"&anction=deleteData";
            for (let i = 0; i < commodityIdList.length; i++) {
                str += "&commodityIdList="+commodityIdList[i]
            }
            $.get(SHOPPING_CART_URL+str,function (result) {
                if(result.status === 200){
                    removeDate();
                    showShoppingCart(result.data);
                    getMoney()
                }else {
                    showErrorBox("网路异常")
                }
            });
        }

    })

    /**
     * 结算
     */
    $('#J_Go').click(function () {
        var flag = false;
        var money = $('#J_Total').text();
        var check = document.getElementsByClassName("checked");
        var str = "";
        for (let i = 0; i < check.length; i++) {
            if(check[i].checked){
                flag = true;
                str += "&cartId="+check[i].getAttribute("name");
            }
        }
        if(flag){
            location.href="pay.html?money="+money+str;
        }else {
            showErrorBox("请选择商品")
        }

    })
    function getMoney() {
        var check = document.getElementsByClassName("checked");
        var vals = document.getElementsByClassName("money");
        var count = 0;
        var sum = 0.0;
        for (let i = 0; i < check.length; i++) {
            if(check[i].checked){
                var number = vals[i].innerHTML;
                sum += parseFloat(number,10);
                count++;
            }
        }
        $('#J_Total').text(sum)
        $("#J_SelectedItemsCount").text(count)
    }
})

function showErrorBox(message) {
    Lobibox.notify('error', {
        size: 'mini',
        delay: 2000,
        delayIndicator: false,
        title: false, //不给title
        msg: message,
        sound: false  // 不要音频
    })
}

